package utils

import (
	_ "github.com/astaxie/beego/orm"
	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"ocane/models"
	"strconv"
)


func init ()  {
	//注册数据库，一共有五个参数，后面连个用于连接池操作
	config := beego.AppConfig
	connIp, _ := config.String("mysql.connect.ip")
	userName, _ := config.String("mysql.connect.userName")
	pwd, _ := config.String("mysql.connect.pwd")
	dbName, _ := config.String("mysql.connect.dbName")
	port, _ := config.Int("mysql.connect.port")
	var connectUrl = userName+":"+pwd+"@tcp("+connIp+":"+strconv.Itoa(port)+")/"+dbName+"?charset=utf8"
	orm.RegisterDataBase("default", "mysql",
		connectUrl)
	log.Println("connect mysql success info ",connectUrl)
	//进行注册模型结构，可以有多个，用逗号分隔
	orm.RegisterModel(new(models.GoUser),new (models.GoJob),new (models.ContextDetail),new (models.ContentDetail),new(models.ContentList),new (models.Company),new (models.BannerImage))
	//创建表，默认为default，只建立一次，后面再执行这个会忽略
	_ = orm.RunSyncdb("default", false, true)
}

